// pages/index/sign.js
var time = "/utils/time.js"
Page({

  /**
   * 页面的初始数据
   */
  data: {
     //初始化年月日
     year: 0,
     month: 0, 
     date: ['日', '一', '二', '三', '四', '五', '六'],
     dateArr: [],
     isToday: [],
     isTodayWeek: false, 
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.init()
    this.getSignData()
  },

 
  init : function(){
    //获取当前日期
    let now = new Date();
    let year = now.getFullYear();
    let month = now.getMonth() + 1;
    this.dateInit();
    this.setData({
      year: year,
      month: month,
    })
  },
  
  //获取签到列表
  getSignData: function() {
    var dataArray = [{"sign_time":"1587367386"},{"sign_time":"1586937453"},{"sign_time":"1585704891"},{"sign_time":"1585623655"},{"sign_time":"1585291675"},{"sign_time":"1585014197"},{"sign_time":"1584947763"},{"sign_time":"1584947730"},{"sign_time":"1584947713"},{"sign_time":"1582792567"},{"sign_time":"1579404181"},{"sign_time":"1579250184"},{"sign_time":"1579056258"},{"sign_time":"1578877416"},{"sign_time":"1578619749"},{"sign_time":"1578533413"},{"sign_time":"1578479831"},{"sign_time":"1578360671"},{"sign_time":"1578275664"},{"sign_time":"1578032758"},{"sign_time":"1577934870"},{"sign_time":"1577754441"},{"sign_time":"1577670165"},{"sign_time":"1577436593"},{"sign_time":"1577339322"},{"sign_time":"1577083170"},{"sign_time":"1576567052"},{"sign_time":"1576307821"},{"sign_time":"1576221412"},{"sign_time":"1576135002"},{"sign_time":"1576048591"},{"sign_time":"1575962178"},{"sign_time":"1575875764"},{"sign_time":"1575788488"},{"sign_time":"1575705872"}]
    let array = new Array()
      for(var i=0;i<dataArray.length;i++){
        array.push(time.formatTime2Date(dataArray[i].sign_time))
      }
    this.setData({
        isToday : array
      })
    },
   
dateInit: function(setYear, setMonth) {
    //全部时间的月份都是按0~11基准，显示月份才+1
    let dateArr = []; //需要遍历的日历数组数据
    let arrLen = 0; //dateArr的数组长度
    let now = setYear ? new Date(setYear, setMonth) : new Date();
    let year = setYear || now.getFullYear();
    let nextYear = 0;
    let month = setMonth || now.getMonth(); //没有+1方便后面计算当月总天数
    let nextMonth = (month + 1) > 11 ? 1 : (month + 1);
    let startWeek = new Date(year + '/' + (month + 1) + '/' + 1).getDay(); //目标月1号对应的星期几
    let dayNums = new Date(year, nextMonth, 0).getDate(); //获取目标月有多少天
    let obj = {};
    let num = 0;
    if (month + 1 > 11) {
      nextYear = year + 1;
      dayNums = new Date(nextYear, nextMonth, 0).getDate();
    }
    arrLen = startWeek + dayNums;
    //这里月份从 0 开始 所以+1
    month+=1
    //这里月份本地时间戳转换日期为01月所以判断一下前面加个0 和具体天数一样的操作
    if(month<=9){
      month = '0'+month
    }
    for (let i = 0; i < arrLen; i++) {
      if (i >= startWeek) {
        num = i - startWeek + 1;
        if(num <= 9){
          num = '0' + num
        }
        obj = {
          isToday:  year +'年'+ month +'月' + num +'日',
          dateNum: num,
          weight: 5
        }
      } else {
        obj = {};
      }
      dateArr[i] = obj;
    }
    this.setData({
      dateArr: dateArr
    })
 
    let nowDate = new Date();
    let nowYear = nowDate.getFullYear();
    let nowMonth = nowDate.getMonth() + 1;
    let nowWeek = nowDate.getDay();
    let getYear = setYear || nowYear;
    let getMonth = setMonth >= 0 ? (setMonth + 1) : nowMonth;
 
    if (nowYear == getYear && nowMonth == getMonth) {
      this.setData({
        isTodayWeek: true,
        todayIndex: nowWeek
      })
    } else {
      this.setData({
        isTodayWeek: false,
        todayIndex: -1
      })
    }
  },
 
  lastMonth: function() {
    //全部时间的月份都是按0~11基准，显示月份才+1
    let year = this.data.month - 2 < 0 ? this.data.year - 1 : this.data.year;
    let month = this.data.month - 2 < 0 ? 11 : this.data.month - 2;
    this.setData({
      year: year,
      month: (month + 1)
    })
    this.dateInit(year, month);
  },
 
  nextMonth: function() {
    //全部时间的月份都是按0~11基准，显示月份才+1
    let year = this.data.month > 11 ? this.data.year + 1 : this.data.year;
    let month = this.data.month > 11 ? 0 : this.data.month;
    this.setData({
      year: year,
      month: (month + 1)
    })
    this.dateInit(year, month);
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
